<?php

// +-----------------------------------------------------
// | Name: 星空框架 操作日志模型
// +-----------------------------------------------------
// | Version: V5.0
// +-----------------------------------------------------
// | Author: xkyinzhe <zhonghuaxinxing@sina.cn>
// +-----------------------------------------------------
// | Time: 2021-04-21 18:14
// +-----------------------------------------------------
declare(strict_types=1);

namespace app\common\model;

use think\model\concern\SoftDelete;

/**
 * @xk 操作日志模型
 */
class AdminLog extends BaseModel
{
    use SoftDelete;
    protected $name = 'adminLog';
    protected $pk = 'admin_log_id';
    protected $deleteTime = 'delete_time';

    /**
     * @xk 写入前操作
     *
     * @method onBeforeWrite
     *
     * @param [type] $data [description]
     *
     * @return [type] [description]
     * @Author xkyinzhe                <zhonghuaxinxing@sina.cn>
     * @Time   2021-04-21T18:15:21+080
     */
    public static function onBeforeWrite($data)
    {
        unset($data['act']);
        unset($data['controllers']);
        unset($data['actions']);
        if (!$data->create_time) {
            $data->update_time = $data->create_time = date('Y-m-d H:i:s');
        }
        $data->update_time = date('Y-m-d H:i:s'); // 操作时间 实际发布时间
    }

    public static function saveLog($data)
    {
        $authRuleModel = new AuthRule();
        $now_url = $data['msg'];
        $data['ip'] = getRealIP();
        $auth_name = $authRuleModel->where(['linkurl' => $now_url])->value('auth_name'); //操作节点名称
        $data['msg'] = $data['admin_name'].'在'.date('Y-m-d H:i:s').'操作了'.$auth_name;
        $data['linkurl'] = $now_url;
        self::create($data);
    }
}
